package com.example.photography.fragment.home;

import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.adapter.product.MyCourseAdapter;
import com.example.photography.bean.CourseBean;
import com.example.photography.common.BaseFragment;
import com.example.photography.util.Util;
import com.google.android.material.snackbar.Snackbar;
import com.loopj.android.http.RequestParams;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.util.BannerUtils;

import butterknife.BindView;

public class Home_course extends BaseFragment {
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.ll_new)
    LinearLayout llNew;
    @BindView(R.id.ll_hot)
    LinearLayout llHot;
    @BindView(R.id.ll_discount)
    LinearLayout llDiscount;
    @BindView(R.id.ll_mine)
    LinearLayout llMine;
    @BindView(R.id.tv_checkall)
    TextView tvCheckall;
    @BindView(R.id.lv_course)
    ListView lvCourse;

    private CourseBean courseBean;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    protected void initData(String content) {
        content = Util.getJson(getActivity(), "home_course.json");
        if (TextUtils.isEmpty(content)) {//数据为空
            return;
        }
        JSONObject jsonObject = JSON.parseObject(content);
        int status = jsonObject.getInteger("code");
        if (status == 202) {
            String data = jsonObject.getString("data");
            //封装转json数据
            courseBean = JSONObject.parseObject(data, CourseBean.class);
            initView();
        } else {
            // TODO: 2021/5/2 加没数据的判断
        }
    }

    private void initView() {
        banner.setAdapter(new BannerImageAdapter<CourseBean.BannerListBean>(courseBean.getBanner_list()) {
            @Override
            public void onBindView(BannerImageHolder holder, CourseBean.BannerListBean banner_list, int position, int size) {
                //图片加载自己实现
                Glide.with(holder.itemView)
                        .load(banner_list.getCover())
                        .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(holder.imageView);
            }
        });
        banner.setIndicator(new RectangleIndicator(getActivity()))//设置指示器
                .setIndicatorSelectedWidth((int) BannerUtils.dp2px(12))
//                        .addBannerLifecycleObserver(this)//添加生命周期观察者(调用banner的addBannerLifecycleObserver()方法，让banner自己控制)
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(Object data, int position) {
                        Snackbar.make(banner, "course_id:"+((CourseBean.BannerListBean) data).getCourse_id(), Snackbar.LENGTH_SHORT).show();
                    }
                });

        MyCourseAdapter myContestAdapter = new MyCourseAdapter(courseBean.getCourse_list(),getActivity(), false);
        lvCourse.setAdapter(myContestAdapter);
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_course;
    }
}
